package gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JList;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;

import model.ProductType;
import model.Process;

import service.Service;

public class ManageData extends JFrame {
	private final Controller controller = new Controller();
	private MainFrame owner;
	private JButton btnAddType, btnEditType, btnRemove, btnAddProcess, btnEditProcess, btnRemoveProcess;
	private JList lstTypes, lstProcesses;

	public ManageData(MainFrame owner) {
		this.owner = owner;
		setSize(567, 461);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(100, 100);
		setResizable(false);
		setVisible(true);
		setTitle("Manage data");
		getContentPane().setLayout(new BorderLayout(5, 5));
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(10, 1, 0, 0));
		
		btnAddType = new JButton("Add Type");
		panel.add(btnAddType);
		
		btnEditType = new JButton("Edit Type");
		panel.add(btnEditType);
		
		btnRemove = new JButton("Remove Type");
		panel.add(btnRemove);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new GridLayout(10, 1, 0, 0));
		
		btnAddProcess = new JButton("Add Process");
		panel_1.add(btnAddProcess);
		
		btnEditProcess = new JButton("Edit Process");
		panel_1.add(btnEditProcess);
		
		btnRemoveProcess = new JButton("Remove Process");
		panel_1.add(btnRemoveProcess);
		
		JPanel panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(0, 2, 5, 0));
		
		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_3.add(panel_5, BorderLayout.NORTH);
		
		JLabel lblProductTypes_1 = new JLabel("Product Types");
		panel_5.add(lblProductTypes_1);
		
		JScrollPane scrollPane = new JScrollPane();
		panel_3.add(scrollPane, BorderLayout.CENTER);
		
		lstTypes = new JList(Service.getInstance().getProductTypes().toArray());
		
		scrollPane.setViewportView(lstTypes);
		
		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_6 = new JPanel();
		panel_4.add(panel_6, BorderLayout.NORTH);
		
		JLabel lblProcesses = new JLabel("Processes");
		panel_6.add(lblProcesses);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_4.add(scrollPane_1, BorderLayout.CENTER);
		
		lstProcesses = new JList(Service.getInstance().getProcesses().toArray());
		
		scrollPane_1.setViewportView(lstProcesses);

		btnAddType.addActionListener(controller);
		btnEditType.addActionListener(controller);
		btnRemove.addActionListener(controller);
		
		btnAddProcess.addActionListener(controller);
		btnEditProcess.addActionListener(controller);
		btnRemoveProcess.addActionListener(controller);
	}

	public void updateLists(){
		lstTypes.setListData(Service.getInstance().getProductTypes().toArray());
		lstProcesses.setListData(Service.getInstance().getProcesses().toArray());
	}
	
	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == btnAddType){
				AddEditType aet = new AddEditType(ManageData.this);
			}
			else if(e.getSource() == btnEditType){
				Object selected = lstTypes.getSelectedValue();
				AddEditType aet;
				
				if(selected != null)
					aet = new AddEditType(ManageData.this, (ProductType) selected);
				else{
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
					
			}
			else if(e.getSource() == btnRemove){
				Object selected = lstTypes.getSelectedValue();
				
				if(selected != null){
//					if(((ProductType) selected).getStacks().size() == 0){
						Service.getInstance().removeProductType((ProductType) selected);
						
						updateLists();
//					}
//					else{
//						JOptionPane.showMessageDialog(ManageData.this,
//								"There should be no stacks associated with this type", "Error",
//								JOptionPane.ERROR_MESSAGE);
//					}
				}
				else{
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(e.getSource() == btnAddProcess){
				AddEditProcess aep = new AddEditProcess(ManageData.this);
			}
			else if(e.getSource() == btnEditProcess){
				Object selected = lstProcesses.getSelectedValue();
				AddEditProcess aep;
				
				if(selected != null)
					aep = new AddEditProcess(ManageData.this, (Process) selected);
				else{
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(e.getSource() == btnRemoveProcess){
				Object selected = lstProcesses.getSelectedValue();
				
				if(selected != null){
					Service.getInstance().removeProcess((Process) selected);
					updateLists();
				}
				else{
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}

	}
}
